package com.daoxuehao.baseframework.model.api;

import com.daoxuehao.baseframework.model.beans.BaseResponse;
import com.daoxuehao.baseframework.model.beans.IpInfo;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okio.Buffer;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Yale on 2016/3/28.
 */
public class ApiManager {
    private static final String ENDPOINT = "http://ip.taobao.com";
    static Retrofit retrofit =null;
    static ApiService apiService = null;



    private ApiManager(){

        Interceptor interceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder().addHeader("User-Agent", "Retrofit-Sample-App").build();

                RequestBody rt = chain.request().body();
                String rs="";

                try {
                    final RequestBody copy = rt;
                    final Buffer buffer = new Buffer();
                    copy.writeTo(buffer);
                    rs =  buffer.readUtf8();
                }
                catch (final IOException e) {
                }

                return chain.proceed(newRequest);
            }
        };

        OkHttpClient client = new OkHttpClient();
        client.interceptors().add(interceptor);

        retrofit = new Retrofit.Builder()
                .baseUrl(ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();

        apiService = retrofit.create(ApiService.class);
    }

    private static class SingleHolder{
        private  static final ApiManager INSTANCE = new ApiManager();
    }

    public  static ApiManager getInstance(){
        return  SingleHolder.INSTANCE;
    }
    private class HttpResultFunc<T> implements Func1<BaseResponse<T>, T> {

        @Override
        public T call(BaseResponse<T> httpResult) {
            if (httpResult.getCode() != 0) {


            }
            return httpResult.getData();
        }
    }


    private void subscribeOnUIThread(Observable obs,Subscriber sub){

        obs.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sub);
    }

    private void subscribeOnIOThread(Observable obs,Subscriber sub){

        obs.subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(sub);
    }
    public  void getIpInfo(Subscriber<IpInfo> subscriber, String ip){


        Map<String ,String > map = new HashMap<String,String>();
        map.put("ip", ip);
        subscribeOnUIThread(apiService.getIpInfo(map)
                .map(new HttpResultFunc<IpInfo>()),subscriber);
    }



}



